package Baseliner::Parse::Eclipse;

use base Baseliner::Base;
use Baseliner::Utils;

use strict;
use Carp;
use File::Find;
use Error qw(:try);

sub new {
	my ($class,$dir,$locale)=@_;
	$dir=~s/\s//g; 
	$dir.="/"; 
	$dir=slashFwd($dir);
	$dir=~s{//}{/}g;
	confess _loc("$class: the workspace directory '%1' does not exist", $dir) if( ! -e $dir );
	my %W = discoverWorkspace( $dir );
	confess _loc("$class: the workspace directory '%1' is empty:", $dir).keys %W if( ! keys %W );

	my $this = {
		DIR=> $dir,
		W => \%W,
		root => \%W
	};
	bless( $this, $class);
}

=head2 discoverWorkspace

Returns the XML snippet for packaging JARs

=cut
sub discoverWorkspace {
	my ($wks) = @_;
	my $wksLen = length($wks);
	my %W=(); ## el workspace
	find( 
		sub { 
			my $full="$File::Find::dir/$_";
			#print $File::Find::name;
			if( $File::Find::name =~ m{/\.project$|/\.classpath$|/application\.xml$|MANIFEST\.MF$|/\.websettings$|/\.modulemaps$|/\.settings/org} ) {
				next if( -d $File::Find::name);
				my $prj = slashFwd(substr( $File::Find::name, $wksLen )); $prj =~ s{^(.*?)/.*$}{$1}g;
				my $class=$_; #$class=~s{^\.(.*)$}{$1}g;
				$W{$prj}{"$class"}{path}=slashFwd($File::Find::name);
				$W{$prj}{"$class"}{dir}=slashFwd($File::Find::dir);
				$W{$prj}{"$class"}{filename}=slashFwd($_);
				my $rel=substr($W{$prj}{"$class"}{path}, $wksLen );
				$W{$prj}{"$class"}{relpath}=$rel;
				## parse file
				open FF,"<$W{$prj}{$class}{path}" or confess(_loc("discoverWorkspace: could not open files %1: %2",$W{$prj}{$class}{path},$!));
				$W{$prj}{"$class"}{data}.=$_ while(<FF>);
				close FF;
				## parse XML
				try {
					my $XML = XML::Smart->new($W{$prj}{"$class"}{data}) or confess _loc("discoverWorkspace: could not parse file %1: %2", $W{$prj}{$class}{path}, $!);
					$W{$prj}{"$class"}{xml}=\$XML if($XML);					
				} otherwise { ##xml invalido, o no era xml: ignora 
				};
			}
		}, $wks );
	return %W;
}

sub getSubsetWorkspace {
	my $this = shift();
	my %W=%{$this->{root}};
	my %NEW_W = ();
	return $this if( ! @_ );
	for(@_) {
		$NEW_W{ $_ }=$W{$_};	
	}
	return \%NEW_W;
}

sub cutToSubset {
	my $this = shift();
	$this->{root} = $this->getSubsetWorkspace(@_);
}

sub reset {
	my $this = shift();
	$this->{root} = $this->{W};
}

1;

__END__

=head1 NAME

Baseliner::Parse::Eclipse - base abstract class for loading an Eclipse Workspace into a Workspace data structure.

=head1 SYNOPSIS

	## create the object and load files
	my $Wks->Baseliner::Parse::Eclipse::J2EE->new('/anydir/workspace');	
	
	## figure out files in memory
	$Wks->parseWorkspaceJ2EE();

	## generate the build.xml data
	my $xml = $Wks->getBuildXML( 
			CLASSPATH => "/javadir/classes",
			JAVAC => 'source="1.4"'
			);	
	
	## printing is believing
	print $xml->{data};
	
	## save the file
	$xml->save('/dir/build.xml');
	
	## call ant -f /dir/build.xml clean build package

=head1 VERSION

Version 1.0  (Sep 12 2008)

=cut
